using System.Collections.Generic;
using Brouteforce.UserInterface.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace Brouteforce.UserInterface.Abstracts {
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public abstract class AbstractScreen : IScreen {
        protected string ScreenName;
        protected List<AbstractPanel> Panels;
        protected bool Popup;
        protected AbstractPanel CenterPanel;
        protected Rectangle CenterBounds;
        protected Rectangle Bounds;


        protected AbstractScreen(string screenName, bool popup, Rectangle bounds) {
            Popup = popup;
            ScreenName = screenName;
            Panels = new List<AbstractPanel>();

            //var screenBounds = BFGame.Graphics.GraphicsDevice.Viewport.Bounds;
            //CenterRectangle = new Rectangle(0, 0, screenBounds.Width, screenBounds.Height);
            Bounds = bounds;
            CenterBounds = bounds;
        }


        //Todo add the calculation of the others abstract panel locations
        //protected Rectangle CalculatePanelsurroundingRectangle(PanelLocation panelLocation) {
        //    Rectangle panelRectangle;

        //    switch (panelLocation) {
        //        case PanelLocation.Center:
        //            panelRectangle = CenterRectangle;
        //            CenterRectangle = new Rectangle(0, 0, 0, 0);
        //            break;
        //        case PanelLocation.Bottom:
        //            panelRectangle = new Rectangle(0, CenterRectangle.Height * 7 / 8, CenterRectangle.Width,
        //                                           CenterRectangle.Height / 8);
        //            CenterRectangle = new Rectangle(0, 0, CenterRectangle.Width,
        //                                               CenterRectangle.Height - panelRectangle.Height);
        //            break;
        //        case PanelLocation.Right:
        //            panelRectangle = new Rectangle(CenterRectangle.Width * 7 / 8, 0, CenterRectangle.Width / 8,
        //                                           CenterRectangle.Height);
        //            CenterRectangle = new Rectangle(0, 0, CenterRectangle.Width - panelRectangle.Width,
        //                                               CenterRectangle.Height);
        //            break;
        //        default:
        //            panelRectangle = CenterRectangle;
        //            CenterRectangle = new Rectangle(0, 0, 0, 0);
        //            break;
        //    }

        //    return panelRectangle;
        //}


        public void AddPanel(AbstractPanel panel, PanelLocation location, int size) {
            Rectangle pbounds = Bounds;
            switch (location) {
                case PanelLocation.Right:
                    pbounds.Width = size;
                    pbounds.X = Bounds.X + Bounds.Width - size;
                    CenterBounds.Width -= size;
                    break;
                case PanelLocation.Left:
                    pbounds.Width = size;
                    CenterBounds.X = size;
                    CenterBounds.Width -= size;
                    break;
                case PanelLocation.Center:
                    CenterPanel = panel;
                    break;
                case PanelLocation.Top:
                    pbounds.Height = size;
                    CenterBounds.Y = size;
                    CenterBounds.Height -= size;
                    break;
                case PanelLocation.Bottom:
                    pbounds.Height = size;
                    pbounds.Y = Bounds.Y + Bounds.Height - size;
                    CenterBounds.Height -= size;
                    break;
            }

            panel.Bounds = pbounds;
            //panel.AfterLayout();
            if (CenterPanel != null) CenterPanel.Bounds = CenterBounds;
            Panels.Add(panel);
        }

        public void Initialize() {

        }

        //public static void LoadContent(ContentManager content) {
            //Panels.ForEach(p => p.LoadContent(content));
        //}

        public void Update(GameTime gameTime) {
            foreach (var panel in Panels) {
                panel.Update(gameTime);
            }
        }

        public void Draw(GameTime gameTime) {
            foreach (var panel in Panels) {
                panel.Draw(gameTime, BFGame.SpriteBatch);
            }
        }

        public List<AbstractPanel> GetPanels() {
            return Panels;
        }

        public bool IsPopup() {
            return Popup;
        }

        public void AfterLayout() {
            foreach (AbstractPanel panel in Panels) {
                panel.AfterLayout();
            }
        }

        //public abstract void Initialize();
    }
}
